/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.filter;

import com.model.LoginModel;
import com.controller.bean.Login;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Chung
 */
public class loginFilter implements Filter {

    private final String LOGINPAGE = "/faces/login.xhtml";
    private final String HOMEPAGE = "/faces/Home.xhtml";
    private final String LOGINSESSION = "login";
    private final String USERNAME = "userName";
    private final String PASSWORD = "passwd";
    private String username;
    private String password;
    private String fullname;
    private String url = "";
    private final String DEFAULTPAGE = "/BluePumpkin/";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;

        getValueCookie(req);
        if (!checkUser(req)) {
            if (!checkUrl(req)) {
                redirect(resp, req, LOGINPAGE);
            } else {
                chain.doFilter(req, resp);
            }
        } else {
            if (checkUrl(req)) {
                redirect(resp, req, HOMEPAGE);

            } else {
                chain.doFilter(req, resp);
            }
        }

    }

    @Override
    public void destroy() {

    }

    private Cookie[] checkCookie(HttpServletRequest req) {
        Cookie[] cookie = req.getCookies();
        return cookie;
    }

    public void getValueCookie(HttpServletRequest req) throws IOException {
        String cookieName;
        Cookie cookie[] = checkCookie(req);
        if (cookie != null && cookie.length > 0) {
            for (Cookie cookie1 : cookie) {
                cookieName = cookie1.getName();
                switch (cookieName) {
                    case USERNAME:
                        username = cookie1.getValue();
                        break;
                    case PASSWORD:
                        password = cookie1.getValue();
                        break;
                }
            }
            if (username != null && password != null) {
                checkAcc(username, password, req);
            }

        }

    }

    private void checkAcc(String username, String password, HttpServletRequest req) throws IOException {
        LoginModel model = new LoginModel();
        fullname = model.loginControl(username, password);
        if (fullname != null) {
            saveLoginToSession(req);

        }
    }

    private boolean checkUrl(HttpServletRequest req) {
        url = req.getRequestURI();
        if (url.equals(DEFAULTPAGE)) {
            return true;
        }
        return url.contains(LOGINPAGE);
    }

    private void redirect(HttpServletResponse resp, HttpServletRequest req, String url) throws IOException {
        resp.sendRedirect(req.getServletContext().getContextPath() + url);
    }

    private boolean checkUser(HttpServletRequest req) throws IOException {

        HttpSession session = req.getSession(false);
        Login useruser = (session != null) ? (Login) session.getAttribute(LOGINSESSION) : null;
        if (useruser != null) {
            return (useruser.getFullname() != null);
        }
        return false;
    }

    private void saveLoginToSession(HttpServletRequest req) throws IOException {
        HttpSession session = ((HttpServletRequest) req).getSession();
        Login user = new Login();
        user.setFullname(fullname);
        session.setAttribute(LOGINSESSION, user);
    }
}
